package com.zyh.scs.admin.controller;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.zyh.scs.admin.api.dto.DeductionDTO;
import com.zyh.scs.admin.api.dto.RechargeBatchJsonDTO;
import com.zyh.scs.admin.api.dto.RechargePhoneDTO;
import com.zyh.scs.admin.api.entity.SysUser;
import com.zyh.scs.admin.api.vo.ReadExcelVO;
import com.zyh.scs.admin.service.SysUserService;
import com.zyh.scs.admin.service.UserPsnInfoService;
import com.zyh.scs.admin.service.UserRechargeService;
import com.zyh.scs.admin.service.impl.temporary.PullFanWeiTemporaryService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.AllArgsConstructor;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.math.BigDecimal;
import java.time.LocalDateTime;
import java.util.List;

/**
 * @author hwh
 * @ClassName: 泛微测试controller$
 * @Description: $
 * @Date $
 * @Version 1.0
 */
@RestController
@AllArgsConstructor
@RequestMapping("/psn")
@Tag(description = "psn", name = "泛微测试controller")
public class UserPsnInfoController {

    private final PullFanWeiTemporaryService pullFanWeiTemporaryService;

    private final UserPsnInfoService userPsnInfoService;

    private final UserRechargeService userRechargeService;

    private final SysUserService sysUserService;

    /**
     * 拉取泛微部门 人员 任职
     */
    @GetMapping("/pullFanWei")
    @Operation(summary = "拉取泛微部门 人员 任职", description = "拉取泛微部门 人员 任职")
    public void pullFanWei() {
        //泛微临时表
        pullFanWeiTemporaryService.pullFanWei();

//        return "success";
        //更新到表中
        pullFanWeiTemporaryService.insertNew();
        //更新用户
        pullFanWeiTemporaryService.updatePsnUser();
        //泛微预警
        pullFanWeiTemporaryService.warmUser();
    }

    /**
     * 拉取泛微部门 人员 任职
     */
    @GetMapping("/lockControl")
    @Operation(summary = "拉取泛微部门 人员 任职", description = "拉取泛微部门 人员 任职")
    public void lockControl() {
        //泛微预警
        pullFanWeiTemporaryService.pullAccount();
    }

    /**
     * 批量充值解析文件
     *
     * @param multipartFile 参数集
     * @return 结果集
     */
    @PostMapping("/read")
    @Operation(summary = "批量充值解析文件", description = "批量充值解析文件")
    public Boolean read(MultipartFile multipartFile) {
        ReadExcelVO read = userRechargeService.read(multipartFile);
        RechargeBatchJsonDTO rechargeBatchJsonDTO = new RechargeBatchJsonDTO();
        rechargeBatchJsonDTO.setFileName(read.getFileName());
        rechargeBatchJsonDTO.setRechargeBatchJsonList(read.getRechargeBatchDTOList().toString());
        rechargeBatchJsonDTO.setFileOldPath(read.getFileOldPath());
        userRechargeService.rechargeBatch(rechargeBatchJsonDTO);
        return Boolean.TRUE;
    }

    /**
     * 通过 页码和页数  type 1为部门  2为人员  3任职
     */
    @GetMapping("/pullToSize")
    @Operation(summary = "通过 页码和页数  type 1为部门  2为人员  3任职", description = "通过 页码和页数  type 1为部门  2为人员  3任职")
    public void pullToSize(Integer pageIndex, Integer pageSize, Integer type) {
        //生成交易流水
        pullFanWeiTemporaryService.pullToSize(pageIndex, pageSize, type);
    }


    /**
     * 更新到表中
     */
    @GetMapping("/insertNew")
    @Operation(summary = "更新到表中", description = "更新到表中")
    public void insertNew() {
        //生成交易流水
        pullFanWeiTemporaryService.insertNew();
    }


    /**
     * 同步泛微用户预警数据
     */
    @GetMapping("/updatePsnUser")
    @Operation(summary = "同步泛微用户预警数据", description = "同步泛微用户预警数据")
    public void updatePsnUser() {
        //生成交易流水
        pullFanWeiTemporaryService.updatePsnUser();
    }

    /**
     * 同步泛微用户预警数据
     */
    @GetMapping("/warmUser")
    @Operation(summary = "同步泛微用户预警数据", description = "同步泛微用户预警数据")
    public void warmUser() {
        //生成交易流水
        pullFanWeiTemporaryService.warmUser();
    }


    /**
     * 泛微测试controller
     */
    @GetMapping("/zhangDan")
    @Operation(summary = "泛微测试controller", description = "泛微测试controller")
    public void zhangDan(LocalDateTime now) {
        //生成交易流水
        userPsnInfoService.insertTransaction(now);
        //生成退款流水
        userPsnInfoService.insertDeduction(now);
        //生成餐补流水
        userPsnInfoService.insertCard(now);
        //生成现金流水
        userPsnInfoService.insertCash(now);
        //生成月末流水
        userPsnInfoService.insertCardAll(now);
    }


    /**
     * 泛微测试controller
     */
    @GetMapping("/zhangDanTo")
    @Operation(summary = "泛微测试controller", description = "泛微测试controller")
    public void zhangDanTo(LocalDateTime now) {
        //生成交易流水
        userPsnInfoService.insertBusinessAccount(now);
        userPsnInfoService.insertPayMKBAccount(now);
        userPsnInfoService.insertPayJDAccount(now);
        userPsnInfoService.insertPayHDAccount(now);
        userPsnInfoService.insertMKBRechargeAccount(now);
        userPsnInfoService.insertBackend(now);
        userPsnInfoService.insertAccountPay(now);
        userPsnInfoService.insertJdBalanceAccount(now);
    }

    /**
     * 脚本  执行充值和扣款
     */
    @GetMapping("/jBen")
    public void jBen() {
        List<SysUser> sysUserList = sysUserService
                .list(Wrappers.<SysUser>lambdaQuery()
                        .eq(SysUser::getIsAdmin, 1));

        for (SysUser sysUser : sysUserList) {

            if (sysUser == null) {
                continue;
            }

            if (StrUtil.isBlank(sysUser.getGuid())) {
                continue;
            }

            //充值
            RechargePhoneDTO rechargePhoneDTO = new RechargePhoneDTO();
            rechargePhoneDTO.setGuid(sysUser.getGuid());
            rechargePhoneDTO.setRechargeNumber(new BigDecimal("100.12"));
            rechargePhoneDTO.setRechargeType("1");
            rechargePhoneDTO.setNotes("餐卡测试充值");
            userRechargeService.recharge(rechargePhoneDTO);

            RechargePhoneDTO rechargePhoneCashDTO = new RechargePhoneDTO();
            rechargePhoneCashDTO.setGuid(sysUser.getGuid());
            rechargePhoneCashDTO.setRechargeNumber(new BigDecimal("200.12"));
            rechargePhoneCashDTO.setRechargeType("2");
            rechargePhoneCashDTO.setNotes("现金测试充值");
            userRechargeService.recharge(rechargePhoneCashDTO);

            //扣费
            DeductionDTO deductionDTO = new DeductionDTO();
            deductionDTO.setGuid(sysUser.getGuid());
            deductionDTO.setDeductionMoney(new BigDecimal("10.12"));
            deductionDTO.setDeductionType(1);
            deductionDTO.setNotes("餐卡测试扣费");
            sysUserService.getDeduction(deductionDTO);

            DeductionDTO deductionCashDTO = new DeductionDTO();
            deductionDTO.setGuid(sysUser.getGuid());
            deductionDTO.setDeductionMoney(new BigDecimal("20.12"));
            deductionDTO.setDeductionType(2);
            deductionDTO.setNotes("现金测试扣费");
            sysUserService.getDeduction(deductionCashDTO);
        }

    }
}
